package edu.iscas.xcheck.analysis.dom.visitors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.iscas.xcheck.analysis.dom.model.DomNode;

public class LevelAssignVisitor extends DomVisitor {
	
	private static final String SPLIT_STR = "/";
	
	private Map<Integer,List<DomNode>> levels;
	

	public LevelAssignVisitor() {
		init();
	}

	public void init() {
		levels = new HashMap<Integer, List<DomNode>>();
	}

	@Override
	public void visit(DomNode node) {
		//assing level base on xpath
		String xpath = node.getxPath();
		if(xpath == null)
			return;
		String[] xpaths = xpath.split(SPLIT_STR);
		int level = xpaths.length;
		List<DomNode> domNodesLevel = levels.get(level);
		if(domNodesLevel == null){
			domNodesLevel = new ArrayList<DomNode>();
			levels.put(level, domNodesLevel);
		}
		node.setLevel(level);
		domNodesLevel.add(node);
	}
	
	public Map<Integer, List<DomNode>> getLevels() {
		return levels;
	}


}
